 package com.itheima.ssm.dao;

import com.itheima.ssm.domain.Member;
import com.itheima.ssm.domain.Orders;
import com.itheima.ssm.domain.Product;
import org.apache.ibatis.annotations.*;

import java.util.List;

public interface IOderDao {
    //D:\dev\code\框架\框架\mybatis-day03\视频  11. 12. 13.  14

//@Results  替代的是<resultMap> 标签
    //@Result 替代了 <id> 和<result>标签
    //column 数据库的列名
    //property 实体的属性名
    //one  需要使用的@One注解
    //many  需要使用的@Many注解
    //@One 代替了<assocation>标签(匹配 对应的单个 实体对象属性名 类型)  是多表查询的关键  ,在注解中用来指定子查询返回的单一对象 (一对一)
//   @One select属性 指定用来多表查询的sqlmapper

    //@Many 代替了<collection> 标签(类似assocation ,对象集合使用) 是多表查询的关键    在注解中用来指定子查询返回对象集合  (多对一)
    @Select("select * from orders")
    @Results({

            @Result(id = true, property = "id" ,column = "id"),
            @Result(property = "payType", column = "payType"),
            @Result(property = "orderDesc", column = "orderDesc"),
            @Result(property = "product", column = "productId",javaType = Product.class,       //  通过 column(id) 查询 product
                    one = @One(select ="com.itheima.ssm.dao.IProductDao.findById" )) ,

            @Result(  property = "member",  column = "memberId" ,javaType = Member.class,
                    one = @One(select = "com.itheima.ssm.dao.IMemberDao.findById")),

            @Result( property = "travellers", column = "id", javaType = List.class ,
                    many =@Many(select = "com.itheima.ssm.dao.ITravellerDao.findByOid") )
    })
  public  List<Orders> findAll() throws Exception;

/*-----------------------------------------------------------------------------------------------*/
    @Select("select *from orders where id=#{id}")
    @Results({
            @Result(id = true, property = "id" ,column = "id"),
            @Result(property = "product", column = "productId", javaType = Product.class,
                    one = @One(select = "com.itheima.ssm.dao.IProductDao.findById")),

            @Result(property = "member", column = "memberId", javaType = Member.class,
                    one = @One(select = "com.itheima.ssm.dao.IMemberDao.findById")),

            @Result(property = "travellers", column = "id", javaType = List.class,
                    many = @Many(select = "com.itheima.ssm.dao.ITravellerDao.findByOid"))
    })
    public Orders selectById(String id);

/*----------------------------------------------------------------------*/

   /* @Update("update orders set orderNum=#{orderNum} ,orderTime=#{orderTime},peopleCount=#{peopleCount}," +
            "orderDesc=#{orderDesc},payType=#{payType} where id=#{id}")
    @Results({
            @Result(property = "product", column = "productId", javaType = Product.class,
            one = @One(select = "com.itheima.ssm.dao.IProductDao. update")),

            @Result(property = "member", column = "memberId", javaType = Member.class,
                    one = @One(select = "com.itheima.ssm.dao.IMemberDao. update")),

            @Result(property = "travellers", column = "id", javaType = List.class,
                    many = @Many(select = "com.itheima.ssm.dao.ITravellerDao. update"))
    })
    public  void update(Orders orders);
*/


    /*--------------------------------------------------------------------------------*/
    @Delete("delete from orders  where id=#{id}")
    @Results({
            @Result(id = true, property = "id" ,column = "id"),
            @Result(property = "product", column = "productId", javaType = Product.class,
                    one = @One(select = "com.itheima.ssm.dao.IProductDao.deleteById")),

            @Result(property = "member", column = "memberId", javaType = Member.class,
                    one = @One(select = "com.itheima.ssm.dao.IMemberDao.deleteById")),

            @Result(property = "travellers", column = "id", javaType = List.class,
                    many = @Many(select = "com.itheima.ssm.dao.ITravellerDao.deleteById"))
    })
    public void deleteById(String id);
}
